<?php

namespace app\common\model\banxi\grammar;

use app\common\enums\AnswerStatusEnum;
use app\common\enums\DetectionTypeEnum;
use app\common\model\common\BaseModel;
use think\model\concern\SoftDelete;

/**
 * 语法检测
 */
class GrammarDetectionAnswer extends BaseModel
{
    use SoftDelete;

    protected $name = 'banxi_grammar_detection_answer';

    protected $autoWriteTimestamp = true;

    protected $deleteTime = 'del';

    protected $defaultSoftDelete = 0;

    protected $append = [
        'detectionTypeCn',
        'answerStatusCn'
    ];

    protected $type = [
        'id' => 'string',
        'del' => 'string',
        'studentId' => 'string',
        'detectionId' => 'string',
        'questionId' => 'string',
        'createTime' => 'timestamp:Y-m-d H:i:s',
        'updateTime' => 'timestamp:Y-m-d H:i:s',
    ];
    //-----------------------------------------------------------------------------------
    //｜                                   获取器                                       ｜
    //-----------------------------------------------------------------------------------

    public function getDetectionTypeCnAttr($value, $data)
    {
        $value = $value ?: ($data['detectionType'] ?? '');
        return (new DetectionTypeEnum())->getDescription($value) ?? '';
    }

    public function getAnswerStatusCnAttr($value, $data)
    {
        $value = $value ?: ($data['answerStatus'] ?? '');
        return (new AnswerStatusEnum())->getDescription($value) ?? '';
    }

    //-----------------------------------------------------------------------------------
    //｜                                   处理方法                                       ｜
    //-----------------------------------------------------------------------------------


    //-----------------------------------------------------------------------------------
    //｜                                   模型关联                                       ｜
    //-----------------------------------------------------------------------------------

    /**
     * 获取该答案所属的语法检测
     */
    public function detection()
    {
        return $this->belongsTo(GrammarDetection::class, 'detectionId', 'id');
    }

    public function questionInfo()
    {
        return $this->belongsTo(GrammarQuestion::class, 'questionId', 'id');
    }
}